<template>
  <div>
    <el-col :span="9" class="propertyborder-title">
      <a-tooltip>
        <template slot="title">{{ content }}</template>
        {{ title }}
      </a-tooltip>
    </el-col>
    <el-col :span="15" class="propertyborder-content">
      <el-dropdown @command="addColumn">
        <span class="el-dropdown-link">
          添加字段<i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <div v-for="ctrl in filedList" :key="ctrl.bindname">
            <el-dropdown-item :command="ctrl">{{
              ctrl.headname
            }}</el-dropdown-item>
          </div>
        </el-dropdown-menu>
      </el-dropdown>
      <span
        class="el-dropdown-link"
        @click="BatchAddfiled"
        style="padding-top:5px;padding-bottom:5px;cursor:pointer;"
        v-if="fromInfo.designType !== '/buildquestionnaire.html'"
      >
        <i class="fa fa-plus"></i>&nbsp;加载字段
      </span>
    </el-col>
    <el-col :span="24" class="widget-config-container">
      <template>
        <ul>
          <li style="background: lightgray;padding: 2px;margin-bottom:2px;">
            <div style="display:inline-block;width:60%;padding-left:10px;">
              <label style="width:65px;float:left">表头</label>
              <label style="width:65px;float:left;margin-left: 5px;"
                >绑定字段</label
              >
            </div>
            <label>操作</label>
          </li>
        </ul>
        <draggable
          tag="ul"
          :list="columnList"
          v-bind="{
            group: { name: 'options1' },
            ghostClass: 'ghost',
            handle: '.drag-item'
          }"
          handle=".drag-item"
        >
          <li
            v-for="(item, index) in columnList"
            :key="index"
            style="background: lightgray;padding: 2px;margin-bottom:2px;"
          >
            <!-- <el-input placeholder="栅格值" size="mini" style="width: 100px;" type="number" v-model.number="item.span"></el-input> -->
            <div style="display:inline-block;width:60%;padding-left:10px;">
              <input v-model="item.title" style="width:65px;float:left" />
              <input
                v-model="item.dataIndex"
                style="width:65px;float:left;margin-left: 5px;"
                readonly="readonly"
              />
            </div>
            <el-button
              @click="handleButtonRemove(index)"
              circle
              plain
              type="danger"
              size="mini"
              icon="el-icon-minus"
              style="padding: 4px;margin-left: 5px;"
            ></el-button>
            <i
              class="drag-item fa fa-bars"
              style="font-size: 16px;margin: 0 5px;cursor: move;"
            ></i>
          </li>
        </draggable>
      </template>
    </el-col>
  </div>
</template>
<script>
import draggable from 'vuedraggable'
import { basicComponents } from './controlconfig.js'
import { getListHeadCell } from '@/api/index'
export default {
  props: ['title', 'content', 'widgetdata'],
  data() {
    return {
      columnList: [],
      filedList: [],
      fromInfo: this.changeFromInfo(),
      apicode: '',
      fromlable: basicComponents[0]
    }
  },
  components: { draggable },
  methods: {
    addColumn(ctrl) {
      let isadd = true
      this.columnList.forEach(ee => {
        if (ctrl.bindname === ee.dataIndex) {
          isadd = false
        }
      })
      if (isadd === true) {
        const key =
          Date.parse(new Date()) + '_' + Math.ceil(Math.random() * 99999)
        const newcontrllable = JSON.parse(JSON.stringify(this.fromlable))
        newcontrllable.dataIndex = ctrl.bindname
        newcontrllable.width = '120'
        newcontrllable.gridid = this.widgetdata.options.controlId
        newcontrllable.isScopedSlots = true
        newcontrllable.model = 'fromlable_' + key
        newcontrllable.title = ctrl.headname
        newcontrllable.key = key
        this.columnList.push(newcontrllable)
        this.widgetdata.options.columns = this.columnList
      }
    },
    loadfiled() {
      const sourcetype = this.widgetdata.options.sourcetype
      if (
        this.apicode === undefined ||
        this.apicode === null ||
        this.apicode === ''
      ) {
        return
      }
      if (sourcetype === 'apicode') {
        getListHeadCell(this.apicode).then(res => {
          if (res.code === 0 && res.success) {
            this.filedList = res.data
          }
        })
      }
    },
    handleButtonRemove(index) {
      this.columnList.splice(index, 1)
    },
    BatchAddfiled() {
      this.filedList.forEach(ee => {
        this.addColumn(ee)
      })
    }
  },
  created() {
    this.apicode = this.widgetdata.options.sourceApi
    this.columnList = this.widgetdata.options.columns
    this.loadfiled()
  },
  computed: {
    SetChangeFromInfo() {
      return this.changeFromInfo()
    }
  },
  watch: {
    SetChangeFromInfo(val) {
      this.fromInfo = val
    },
    'widgetdata.options.sourceApi': {
      deep: true,
      handler(val) {
        this.apicode = val
        this.columnList = []
        this.widgetdata.options.columns = []
        this.loadfiled()
      }
    }
  },
  inject: ['changeFromInfo', 'setSelectWidget']
}
</script>
<style scoped lang="less">
.propertyborder-title {
  height: 42px;
  line-height: 42px;
  padding-left: 3px;
  border: 1px solid #ccc;
  border-top: none;
  border-right: none;
}

.propertyborder-content {
  border: 1px solid #ccc;
  border-top: none;
  border-right: none;
  padding-right: 3px;
  padding-left: 3px;
  padding-top: 4px;
  padding-bottom: 4px;
  height: 42px;
}
.widget-config-container {
  position: relative;

  .el-header {
    border-bottom: solid 2px #e4e7ed;
    padding: 0px;
  }

  .column-setting .el-form-item__label {
    float: none;
    display: inline-block;
    text-align: left;
    padding: 0 0 2px;
  }

  .config-content {
    border-left: solid 1px #e6e6e6;

    .el-form-item__label {
      padding: 0;
      font-weight: 500;
      float: none;
    }

    .el-form-item {
      /*border-bottom: solid 1px #e1e1e1;
            margin-bottom: 0px;*/
      margin: 5px;
    }
  }

  .ghost {
    background: #fff;
    border: 1px dashed #409eff;

    &::after {
      background: #fff;
      display: block;
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
    }
  }

  ul {
    margin: 0;
    padding: 0;
  }

  li.ghost {
    list-style: none;
    font-size: 0;
    display: block;
    position: relative;
  }
}
</style>
